package com.ysm.smfu.welcome;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ysm.smfu.MainActivity;
import com.ysm.smfu.R;
import com.ysm.smfu.activity.All_LoginActivity;
import com.ysm.smfu.activity.BaseActivity;
import com.ysm.smfu.utils.UIUtils;

//第一次运行的引导页代码
public class WelcomeActivity extends BaseActivity implements OnPageChangeListener,
        OnClickListener {
    private ViewPager viewPager;
    private PagerAdapter pagerAdapter;
    private Button openApp;
    private LinearLayout indicatorLayout;
    private ImageView[] indicators = null;

    private int[] images;
    private Button loginButton;

    @Override
    protected void init() {
        super.init();
        // 设置引导图片
        //设置图片 指示器和page自动添加
        images = new int[]{R.drawable.welcome_01, R.drawable.welcome_02,
                R.drawable.register_login};
    }

    @Override
    protected void initViews() {
        super.initViews();
        setContentView(R.layout.activity_welcome);
        // 创建桌面快捷方式
        new CreateShut(this);
        initView();
    }

    // 初始化视图
    private void initView() {
        // 实例化视图控件
        viewPager = (ViewPager) findViewById(R.id.viewpage);
        openApp = (Button) findViewById(R.id.openApp);
        loginButton = (Button) findViewById(R.id.login);
        indicatorLayout = (LinearLayout) findViewById(R.id.indicator);

        openApp.setOnClickListener(this);
        loginButton.setOnClickListener(this);

        indicators = new ImageView[images.length]; // 定义指示器数组大小
        for (int i = 0; i < images.length; i++) {
            // 循环加入指示器
            indicators[i] = new ImageView(UIUtils.getContext());
            indicators[i].setBackgroundResource(R.drawable.btn_circle_shape);
            if (i == 0) {
                indicators[i].setBackgroundResource(R.drawable.btn_circle_shape_r);
            }
            //指示点之间到间距
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(-2, -2);
            params.leftMargin = UIUtils.dip2px(10);
            indicatorLayout.addView(indicators[i], params);
        }
        pagerAdapter = new BasePagerAdapter(images);
        viewPager.setAdapter(pagerAdapter); // 设置适配器
        viewPager.addOnPageChangeListener(this);
    }

    //按钮的点击事件
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.openApp) {
            UIUtils.startActivity(MainActivity.class);
            overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
        } else if (v.getId() == R.id.login) {
            UIUtils.startActivity(MainActivity.class);
//            传递参数，用于在登录页返回时，清空用户信息
            UIUtils.startActivity(All_LoginActivity.class, "isWec", "isWec");
            overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
        }
        finish();
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    // 监听viewpage
    @Override
    public void onPageSelected(int arg0) {
        // 显示最后一个图片时显示按钮
//        if (arg0 == indicators.length - 1) {
//            btnVisible();
//
//        } else {
//            btnInvisible();
//        }
        // 更改指示器图片
        for (int i = 0; i < indicators.length; i++) {
            indicators[arg0].setBackgroundResource(R.drawable.btn_circle_shape_r);
            if (arg0 != i) {
                indicators[i]
                        .setBackgroundResource(R.drawable.btn_circle_shape);
            }
        }
    }

    /**
     * 按钮隐藏
     */
    private void btnInvisible() {

        invisibleAnimation(1f, 0f, 1f, 0.7f, openApp, loginButton);

    }


    /**
     * 注册按钮显示
     */
    private void btnVisible() {

        visibleAnimation(0f, 1f, 0.7f, 1f, openApp, loginButton);
    }

    /**
     * 隐藏的动画
     *
     * @param start   开始透明度
     * @param end     结束透明度
     * @param scale   开始缩放比例
     * @param endscla 结束缩放比例
     */
    private void invisibleAnimation(float start, float end, float scale, float endscla, final View... view) {

        AlphaAnimation alphaAnimation = new AlphaAnimation(start, end);
        alphaAnimation.setDuration(500);

        for (int i = 0; i < view.length; i++) {
            if (view[i].getVisibility() == View.VISIBLE)

                view[i].startAnimation(alphaAnimation);
        }
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                for (int i = 0; i < view.length; i++) {
                    view[i].setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    /**
     * 显示出来的动画
     *
     * @param start   开始透明度
     * @param end     结束透明度
     * @param scale   开始缩放比例
     * @param endscla 结束缩放比例
     * @param view
     */
    private void visibleAnimation(float start, float end, float scale, float endscla, final View... view) {
        AlphaAnimation alphaAnimation = new AlphaAnimation(start, end);
        alphaAnimation.setDuration(500);

        openApp.setVisibility(View.VISIBLE);
        loginButton.setVisibility(View.VISIBLE);

        for (int i = 0; i < view.length; i++) {
            view[i].startAnimation(alphaAnimation);
        }
    }
}
